#!/usr/bin/env python
#coding: UTF-8

import paramiko

server=[['192.168.115.10','root','yujianglei'],['192.168.115.9','root','yujianglei']]
port=22
def init():
    Loop_count=0
    Input_count=0
    Input_server_number = None
    limit=5
    print "服务器IP列表如下:"
    for ip in server:
        Loop_count+=1
        print  Loop_count,"\t",ip[0]
    while not isinstance(Input_server_number,int):
        try:
            Input_count+=1
            Input_server_number = int(raw_input("请选择一下您要登录的服务器IP编号:"))
        except Exception,msg:
            print "输入的数据类型不正确,请重新输入:"
        if Input_count >= limit:
            print "5次输入错误,退出登录。"
            break

    while Input_server_number not in range(1,Loop_count+1):
        try:
            Input_count+=1
            Input_server_number = int(raw_input("请选择正确的服务器IP编号:"))
            Loop_count=0
            for ip in server:
                Loop_count+=1
                print  Loop_count,"\t",ip[0]
        except Exception,msg:
            print "输入的服务器编号范围有误,请重新输入:"
        if Input_count >= limit:
            print "5次输入错误,退出登录。"
            break
    serial_number = Input_server_number
    connect_server(serial_number)

def connect_server(serial_number):
    server_index = serial_number -1
    server_ip = server[server_index][0]
    print server_ip
    server_user=server[server_index][1]
    print server_user
    server_password=server[server_index][2]
    print server_password
    ssh =  paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=server_ip,username=server_user,password=server_password)
    command = raw_input('请输入要执行的命令:')
    stdin,stdout,stderr = ssh.exec_command(command)
    print stdout.readlines()

init()
